import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    host: '0.0.0.0',
    allowedHosts: ['localhost', '127.0.0.1','dev.my.com'],
    proxy: {
      '/api': {
        target: 'https://xxx.com',
        changeOrigin: true,
        // rewrite: (path) => path.replace(/^\/api/, ''),
        configure: (proxy, options) => {
          // 解决请求403问题：invalid CORS request。非常重要的代码！！
          proxy.on('proxyReq', function (proxyReq, req, res) {
            proxyReq.removeHeader('referer')  // 移除请求头
            proxyReq.removeHeader('origin') // 移除请求头
          })
        },
		    // path是请求方法axios配置的baseUr中去除 协议+域名+端口 剩下的部分。例如http://127.0.0.1:9000/api，这里的path就是/api
        rewrite: (path) => path.replace(/^\/api/, '') // 路径重写
      }
    },
  },
  plugins: [vue()],
  base: '/',  // 设置基础路径
  build: {
    outDir: './dist',  // 输出到Spring Boot的静态资源目录
    emptyOutDir: true,
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes('node_modules')) {
            if(id.includes('element-plus')){
              return 'element-plus';
            }
            return 'vendor'; // 将第三方库打包到 vendor chunk 中
          }
        },
        entryFileNames: 'js/[name].js',
        chunkFileNames: 'js/[name].js',
        assetFileNames: 'css/[name].[ext]',
        chunkSizeWarningLimit: 500
      }
    }
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src')
    }
  }
}) 